﻿using System.Windows.Media.Media3D;
using System.Diagnostics;
using LFSRecord2.Misc;

namespace LFSRecord2.Model.UserActions
{
    public class UserActionKeyFramePoint3DValueChanged : UserActionBase
    {
        // Action description, so we can replay this action (and reverse it)
        public int LayerIndex { get; set; }
        public int PropertyIndex { get; set; }
        public int KeyFrameIndex { get; set; }
        public Vector3D ValueOffset { get; set; }

        public UserActionKeyFramePoint3DValueChanged()
        {
            Action = UserActionTypes.KeyFramePoint3DValueChanged;
        }

        public override void DoUndo()
        {
            Debug.WriteLine("Undoing a KeyFramePoint3DValueChanged action");
            if (KeyFrameIndex == -1)
            {
                ((CompLayerPropertyPoint3D)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex]).CurrentXValue -= ValueOffset.X;
                ((CompLayerPropertyPoint3D)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex]).CurrentYValue -= ValueOffset.Y;
                ((CompLayerPropertyPoint3D)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex]).CurrentZValue -= ValueOffset.Z;
            }
            else
            {
                if (KeyFrameIndex > LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames.Count - 1)
                {
                    Logger.LogError("Invalid keyframe reference while undoing kfPoint3DValueAction");
                }
                CustomPoint3DKeyFrame kfp = (CustomPoint3DKeyFrame)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames[KeyFrameIndex];
                kfp.Value = Point3D.Subtract(kfp.Value, ValueOffset);
            }
            LfsRecordController.project.Layers[LayerIndex].UpdatedCurrentValues = CompLayerPropertyUpdateTypes.Position;
                
        }

        public override void DoRedo()
        {
            Debug.WriteLine("Redoing a KeyFramePoint3DValueChange action");
            if (KeyFrameIndex == -1)
            {
                ((CompLayerPropertyPoint3D)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex]).CurrentXValue += ValueOffset.X;
                ((CompLayerPropertyPoint3D)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex]).CurrentYValue += ValueOffset.Y;
                ((CompLayerPropertyPoint3D)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex]).CurrentZValue += ValueOffset.Z;
            }
            else
            {
                if (KeyFrameIndex > LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames.Count - 1)
                {
                    Logger.LogError("Invalid keyframe reference while redoing kfPoint3DValueAction");
                }
                CustomPoint3DKeyFrame kfp = (CustomPoint3DKeyFrame)LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames[KeyFrameIndex];
                kfp.Value = Point3D.Add(kfp.Value, ValueOffset);
            }
            LfsRecordController.project.Layers[LayerIndex].UpdatedCurrentValues = CompLayerPropertyUpdateTypes.Position;
              
        }
    }
}
